#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWDetailListEntry.h"
#import "framework/FWDetailListSection.h"
#import "framework/FWPlugin.h"

#import "PICommon.h"
#import "PITaskDebug.h"
#import "PIAlarm.h"
#import "PITask.h"
#import "PITaskStatus.h"
#import "PITaskPlugin.h"
#import "PIContext.h"


static NSString* const kAutoContextPage = @"LocationCtx";
static NSString* const kManualContextPage = @"ManualCtx";


@implementation PITaskDebug


@synthesize detailPageID;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (void)dealloc
{
    [detailPageID release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Accessing records from the database

- (NSArray*)detailSectionsForListPage:(NSObject *)page key:(NSObject*)key application:(FWApplication *)application
{  
    PITaskStatus* info = (PITaskStatus*)application.info;
    PITaskPlugin* plugin = (PITaskPlugin*)application.fwPlugin;
    PITaskStatus* status = (PITaskStatus*)application.info;

    NSArray* infoSect = [NSArray arrayWithObjects:
                          [FWDetailListEntry entryWithTitle:@"⚡ Last Update" value:FWDebugTimeStringForUTCDate(plugin.lastUpdate)],
                         [FWDetailListEntry entryWithTitle:@"Total Items"
                                                     value:[[NSNumber numberWithInt:info.itemsReturnedCount] stringValue]],
                          [FWDetailListEntry entryWithTitle:@"Items Filtered"
                                                      value:[[NSNumber numberWithInt:info.itemsFilteredCount] stringValue]],
                          [FWDetailListEntry entryWithTitle:@"Items With Details"
                                                      value:[[NSNumber numberWithInt:info.itemsWithDetailsCount] stringValue]],
                          nil];

    NSMutableArray* contextSect = [NSMutableArray arrayWithCapacity:2];

    [contextSect addObject:[FWDetailListEntry entryWithTitle:@"Location Contexts"
                                                       value:status.isAutoContextAvailable ? @"Available" : @"Unavailable"]];

    if (status.isAutoContextOn)
        [contextSect addObject:[[FWDetailListEntry entryWithTitle:@"Locations"]
                                withLinkType:kAutoContextPage]];
    else
        [contextSect addObject:[FWDetailListEntry entryWithTitle:@"Locations" value:@"Off"]];

    if (status.isFixedContextOn)
        [contextSect addObject:[[FWDetailListEntry entryWithTitle:@"Manual"]
                                withLinkType:kManualContextPage]];
    else
        [contextSect addObject:[FWDetailListEntry entryWithTitle:@"Manual" value:@"Off"]];

    return [NSArray arrayWithObjects:
            [FWDetailListSection sectionForElements:infoSect withTitle:@"Last Update Details"],
            [FWDetailListSection sectionForElements:contextSect withTitle:@"Task Contexts"],
            nil];
}

- (NSString*)captionForListPage:(NSObject *)page forKey:(NSObject *)key displayDepth:(NSInteger)depth application:(FWApplication *)application
{
    NSBundle* bundle = [NSBundle bundleForClass:[self class]];
    
    if ([(NSString*)key isEqualToString:kAutoContextPage])
        return localize(bundle, @"ListView/Locations");
    else if ([(NSString*)key isEqualToString:kManualContextPage])
        return localize(bundle, @"ListView/Contexts");
    else
        return [super captionForListPage:page forKey:key displayDepth:depth application:application];
}

- (NSArray*)listPageInfoForKey:(NSObject *)key currentPage:(NSObject *)page displayDepth:(NSInteger)depth application:(FWApplication *)application
{
    self.detailPageID = (NSString*)key;
    return [NSArray arrayWithObject:page];
}

- (void)willShowBasePage:(NSObject *)page
{
    self.detailPageID = nil;
    
    [super willShowBasePage:page];
}

- (NSArray*)contextListPageForList:(NSArray*)list
{
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:list.count];
    
    for (PIContext* context in list) {
        [items addObject:[FWDetailListEntry entryWithTitle:context.title]];
    }
    
    return [NSArray arrayWithObject:[FWDetailListSection sectionForElements:items]];
}

- (NSArray*)dataForDetailListPage:(NSObject*)page key:(NSObject*)key application:(FWApplication *)application
{
    if ([self.detailPageID isEqualToString:kAutoContextPage])
        return [self contextListPageForList:[application getAttribute:kPIApplicationContextLocation]];
    else if ([self.detailPageID isEqualToString:kManualContextPage])
        return [self contextListPageForList:[application getAttribute:kPIApplicationContextFixed]];
    else
        return [super dataForDetailListPage:page key:key application:application];
}

@end
